package cn.dreamtobe.kpswitch.demo.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import cn.dreamtobe.kpswitch.demo.R;
import cn.dreamtobe.kpswitch.util.KPSwitchConflictUtil;
import cn.dreamtobe.kpswitch.util.KeyboardUtil;
import cn.dreamtobe.kpswitch.widget.KPSwitchPanelLinearLayout;

/**
 * Created by Jacksgong on 15/7/1.
 * <p/>
 * For FragmentActivity/Activity. (Just for test(because has different view hierarchy, but has
 * already handled internal), all calls are identical to the {@link ChattingResolvedActivity}).
 * <p/>
 * For resolving the conflict by delay the visible or gone of panel.
 * <p/>
 * In case of Normal(not fullscreen) Theme.
 * In case of Translucent Status Theme with the {@code getFitSystemWindow()} is true in root view.
 */
public class ChattingResolvedFragmentActivity extends FragmentActivity {

    private static final String TAG = "ResolvedActivity";
    private RecyclerView mContentRyv;
    private EditText mSendEdt;
    private KPSwitchPanelLinearLayout mPanelRoot;
    private TextView mSendImgTv;
    private ImageView mPlusIv;

    private void assignViews() {
        mContentRyv = findViewById(R.id.content_ryv);
        mSendEdt = findViewById(R.id.send_edt);
        mPanelRoot = findViewById(R.id.panel_root);
        mSendImgTv = findViewById(R.id.send_img_tv);
        mPlusIv = findViewById(R.id.plus_iv);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void adaptTheme(final boolean isTranslucentStatusFitSystemWindowTrue) {
        if (isTranslucentStatusFitSystemWindowTrue
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    private void adaptTitle(final boolean isTranslucentStatusFitSystemWindowTrue) {
        if (isTranslucentStatusFitSystemWindowTrue) {
            setTitle(R.string.activity_chatting_translucent_status_true_resolved_title);
        } else {
            setTitle(R.string.activity_chatting_resolved_title);
        }
    }

    private void adaptFitsSystemWindows(final boolean isTranslucentStatusFitSystemWindowTrue) {
        if (isTranslucentStatusFitSystemWindowTrue
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            findViewById(R.id.rootView).setFitsSystemWindows(true);
        }
    }

    private boolean isShowing = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ********* Below code Just for Demo Test, do not need to adapt in your code. ************
        final boolean isTranslucentStatusFitSystemWindowTrue = getIntent().
                getBooleanExtra(MainActivity.KEY_TRANSLUCENT_STATUS_FIT_SYSTEM_WINDOW_TRUE, false);
        adaptTheme(isTranslucentStatusFitSystemWindowTrue);

        setContentView(R.layout.activity_chatting_resolved);

        adaptFitsSystemWindows(isTranslucentStatusFitSystemWindowTrue);

        adaptTitle(isTranslucentStatusFitSystemWindowTrue);

        assignViews();

        if (getIntent().getBooleanExtra(MainActivity.KEY_IGNORE_RECOMMEND_PANEL_HEIGHT, false)) {
            mPanelRoot.setIgnoreRecommendHeight(true);
        }

        KeyboardUtil.attach(this, mPanelRoot, new KeyboardUtil.OnKeyboardShowingListener() {
            @Override
            public void onKeyboardShowing(boolean isShowing) {
                ChattingResolvedFragmentActivity.this.isShowing = isShowing;
                Log.d(TAG, String.format("Keyboard is %s", isShowing ? "showing" : "hiding"));
            }
        });

        KPSwitchConflictUtil.attach(mPanelRoot, mPlusIv, mSendEdt, new KPSwitchConflictUtil.SwitchClickListener() {
            @Override
            public void onClickSwitch(View v, boolean switchToPanel) {
                Log.d(TAG, String.format("SwitchToPanel is %s", switchToPanel ? "showing" : "hiding"));
            }
        });

        mSendImgTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSendEdt.append("you");
            }
        });

        mContentRyv.setLayoutManager(new LinearLayoutManager(this));

        mContentRyv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (isShowing) {
                    KPSwitchConflictUtil.hidePanelAndKeyboard(mPanelRoot);
                }
            }
        });
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (isShowing) {
            if (event.getAction() == KeyEvent.ACTION_UP
                    && event.getKeyCode() == KeyEvent.KEYCODE_BACK
                    && mPanelRoot.getVisibility() == View.VISIBLE) {
                KPSwitchConflictUtil.hidePanelAndKeyboard(mPanelRoot);
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}